package com.xindacloud.sms.portal.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xindacloud.sms.portal.dao.SmsUserAllowOperatorMapper;
import com.xindacloud.sms.portal.dto.PricingDTO;
import com.xindacloud.sms.portal.entity.SmsAgent;
import com.xindacloud.sms.portal.entity.SmsUserAllowOperator;
import com.xindacloud.sms.portal.exception.ServiceException;
import com.xindacloud.sms.portal.service.ISmsAgentService;
import com.xindacloud.sms.portal.service.ISmsUserAllowOperatorService;
import com.xindacloud.sms.portal.utils.UnionResultHelp;
import com.xindacloud.sms.portal.vo.PricingListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class SmsUserAllowOperatorServiceImpl extends ServiceImpl<SmsUserAllowOperatorMapper, SmsUserAllowOperator> implements ISmsUserAllowOperatorService {

    final SmsUserAllowOperatorMapper userAllowOperatorMapper;

    @Autowired
    private ISmsAgentService agentService;

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    public SmsUserAllowOperatorServiceImpl(SmsUserAllowOperatorMapper userAllowOperatorMapper) {
        this.userAllowOperatorMapper = userAllowOperatorMapper;
    }

    @Override
    public IPage<PricingListVO> queryUserPricingPage(PricingDTO pricingDTO) {
        Page<SmsUserAllowOperator> pricingPage = new Page<>(pricingDTO.getCurrentPage(), pricingDTO.getPageSize());
        return userAllowOperatorMapper.selectUserPricingList(pricingPage, pricingDTO);
    }


    @Override
    public List<PricingListVO> downloadPricing(PricingDTO pricingDTO) {
        return userAllowOperatorMapper.downloadUserPricingList(pricingDTO);
    }

    @Override
    public SmsUserAllowOperator queryUserAllowOperatorMinPrice(Integer userId, String countryCode, String prefix) {

        List<SmsUserAllowOperator> minPricingList = lambdaQuery()
                .eq(SmsUserAllowOperator::getStatus, true)
                .eq(SmsUserAllowOperator::getUserId, userId)
                .eq(SmsUserAllowOperator::getCountryCode, countryCode)
                .eq(SmsUserAllowOperator::getPrefix, prefix)
                .orderByAsc(SmsUserAllowOperator::getPrice)
                .last("LIMIT 500")
                .list();

        Optional.of(minPricingList)
                .filter(CollectionUtils::isNotEmpty)
                .orElseThrow(() -> new ServiceException(String.format("该用户未同步当前国家编码为:%s,国家前缀为:%s运营商价格", countryCode, prefix),
                        UnionResultHelp.StatusEnum.ParamsFail.code()));

        final List<SmsUserAllowOperator> filterPriceList = new ArrayList<>();
        //把禁用的代理账户过滤掉
        final List<SmsAgent> smsAgents = agentService.queryAvailableAgentList(minPricingList.stream()
                .map(SmsUserAllowOperator::getAgentId)
                .collect(Collectors.toList()));
        for (SmsUserAllowOperator allowOperator : minPricingList) {
            for (SmsAgent agent : smsAgents) {
                if (Objects.equals(allowOperator.getAgentId(), agent.getAgentId())) {
                    filterPriceList.add(allowOperator);
                }
            }
        }
        if (CollectionUtils.isEmpty(filterPriceList)) {
            throw new ServiceException("没有可用的服务通道或服务已被禁用.", UnionResultHelp.StatusEnum.InnerError.code());
        }


        return filterPriceList.get(0);
    }
}
